<template>
  <div class="container">
    <el-container>
      <el-header>
        <el-row>
          <el-col :span="24" class="flow-model-container">
            <h3 class="title">{{ $t('flowDesign.flowDesignTitle') }}</h3>
            <div class="flow-model-operate">
              <el-input
                :placeholder="$t('category.input')"
                v-model="flowName"
                class="input-with-select"
                @change="searchModelList(1)"
                clearable
              >
                <template #prepend>
                  <el-button icon="el-icon-search" @click="searchModelList(1)"></el-button>
                </template>
              </el-input>
              <el-button
                v-limit="'LCZX:LCKZ:LCMX:ADD'"
                type="primary"
                icon="el-icon-plus"
                @click="toggleList({ flag: false })"
              >
                {{ $t('flowDesign.operate.addFlow') }}</el-button
              >
              <input type="file" ref="fileRef" :style="{ display: 'none' }" @change="openFile" />
              <el-button
                v-limit="'LCZX:LCKZ:LCMX:IMPORT'"
                type="primary"
                icon="el-icon-sold-out"
                :style="{ marginLeft: '8px' }"
                @click="importFlow"
                >{{ $t('flowDesign.operate.importFlow') }}</el-button
              >
            </div>
          </el-col>
        </el-row>
      </el-header>
      <el-main
        class="model-list-container"
        v-loading="listLoading"
        :element-loading-text="$t('common.loading')"
      >
        <el-row :gutter="20">
          <el-col
            :xl="6"
            :md="12"
            :sm="24"
            v-for="(item, i) in $store.state.flowDesignStore.flowListData"
            :key="item.id"
            class="flow-card-container"
          >
            <c-card :cardData="item" :index="i" />
          </el-col>
        </el-row>
        <el-pagination
          background
          layout="total, prev, pager, next"
          :page-size="page.pageSize"
          @current-change="searchModelList"
          v-model:currentPage="page.current"
          :total="$store.state.flowDesignStore.total"
          align="right"
        >
        </el-pagination>
      </el-main>
    </el-container>
  </div>
</template>

<script>
import FlowDescCard from './components/flowDescCard.vue'
import { createNamespacedHelpers, mapState } from 'vuex'
const { mapActions } = createNamespacedHelpers('flowDesignStore')
export default {
  name: 'FlowModelList',
  data() {
    return {
      flowListData: [],
      listLoading: false,
      flowName: '',
      page: {
        current: 1,
        pageSize: 10,
        total: 0,
      },
    }
  },

  methods: {
    ...mapActions(['getFlowList']),
    // 查询流程列表信息
    async searchModelList(current) {
      this.$store.state.flowDesignStore.form.flowName = this.flowName
      const rst = await this.getFlowList({
        params: {
          pageNum: current,
          pageSize: this.page.pageSize,
        },
        data: {
          flowType: this.$store.state.flowDesignStore.form.flowType,
          flowName: this.flowName,
        },
      })
      if (rst.status) {
        this.flowListData = rst.data.records
        this.$store.state.flowDesignStore.flowListData = rst.data.records
        this.$store.state.flowDesignStore.total = rst.data.total
        this.listLoading = false
        // this.flowListData = rst.data.data.records;
        // this.$store.state.flowDesignStore.flowListData = rst.data.data.records;
        // this.$store.state.flowDesignStore.total = rst.data.data.total;
        // this.listLoading = false;
        console.log(this.flowListData, 'this.flowListData')
      } else {
        this.$message({
          type: 'warning',
          message: rst.message,
        })
      }
    },
    // 删除流程列表中的card
    deleteCard(index) {
      this.flowListData.splice(index, 1)
    },
    importFlow() {
      this.$refs.fileRef.click()
    },
    openFile(e) {
      if (e.target.files.length > 0) {
        const file = e.target.files[0]
        const reader = new FileReader()
        reader.readAsText(file)
        reader.onload = (event) => {
          const data = event.target.result
          this.toggleList({ flag: false, bpmnXML: data })
        }
      }
    },
  },
  components: {
    'c-card': FlowDescCard,
  },
  provide() {
    return {
      deleteCard: this.deleteCard,
    }
  },
  inject: ['toggleList'],
  mounted() {
    this.listLoading = true
    this.searchModelList(1)
  },
}
</script>

<style scoped lang="scss">
.el-container {
  width: 100%;
  background: white;
  height: 100%;
  @include flex-style(flex, auto, column);
  .el-header {
    border-bottom: 1px solid #ccc;
    .flow-model-container {
      display: flex;
      justify-content: space-between;
      align-items: center;
      .title {
        font-weight: normal;
      }
      .flow-model-operate {
        display: flex;
        .input-with-select {
          margin-right: 10px;
        }
      }
      .el-main {
        @include flex-style(flex, auto, row);
        #flow {
          flex: auto;
        }
        #js-properties-panel {
          flex: 0 0 300px;
        }
      }
    }
  }
  .model-list-container {
    display: flex;
    flex: auto;
    flex-direction: column;
    .el-row {
      flex: auto;
    }
    .el-pagination {
      margin: 10px 0 0 0;
    }
  }
}
.flow-card-container {
  padding-top: 10px;
}
</style>
